package com;

import com.config.RedisConfig;
import com.config.RootConfig;
import com.pojo.Account;
import com.service.AccountService;
import com.service.RedisService;
import com.service.RedisServiceImpl;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.DynamicTest;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;

import javax.sql.DataSource;
import java.util.concurrent.TimeUnit;

public class AppTest {

    public static void main(String[] args){
//        test3();
//        test2();
//        test1();
        testRedisTemplate();
    }

    private static  void test3(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(RootConfig.class, RedisConfig.class);
        AccountService accountService = ctx.getBean(AccountService.class);
        Account account = accountService.findAccountById(1);
        System.out.println("account1:"+account);

        RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);
        redisTemplate.opsForValue().set("accout", account);
        redisTemplate.expire("accout", 30, TimeUnit.SECONDS);
        System.out.println("account2:"+redisTemplate.opsForValue().get("account"));

    }

    private static void test2(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(RootConfig.class, RedisConfig.class);
        AccountService accountService = ctx.getBean(AccountService.class);
        String test = accountService.test(1);
        System.out.println(test);
    }

    private static void test1(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(RootConfig.class, RedisConfig.class);
        AccountService accountService = ctx.getBean(AccountService.class);
        Account account = accountService.findAccountById(1);
        System.out.println("account1:"+account);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Account account2 = accountService.findAccountById(1);
        System.out.println("account2:"+account2+","+(account==account2));

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Account account3 = accountService.findAccountById(1);
        System.out.println("account3:"+account3+","+(account3==account2));
    }

    private static void testRedisTemplate(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(RootConfig.class, RedisConfig.class);
        RedisService redisService = ctx.getBean(RedisService.class);

        redisService.execMultiCommand();
        redisService.execTransaction();
        redisService.execPipeline();

//        RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);
//        redisTemplate.execute(new SessionCallback() {
//            @Override
//            public Object execute(RedisOperations redisOperations) throws DataAccessException {
//                Object o = redisOperations.boundHashOps("hash").get("k_hash_1");
//                System.out.println("1:"+o);
//                redisOperations.boundHashOps("hash").put("k_hash_1","xxxx");
//
//                Object o1 = redisOperations.boundHashOps("hash").get("k_hash_1");
//                System.out.println("3:"+o1);
//                return null;
//            }
//        });
    }
}
